<?php
namespace app\order\model;
use app\rbac\model\AdminM;
use app\tools\kd\KdSearch;
use think\Request;
use think\Validate;
use think\Db;
use app\base\model\Base;
class OrderM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'order_id' => 'require',
               'logistic_type_id' => 'require|number',
               'logisticCode' => 'require',
           ];
        $msg = [];
        $filed = [
            'order_id'=>'订单ID',
            'logistic_type_id'=>'物流类型',
            'logisticCode'=>'物流单号',
        ];
           $validate = new Validate($rule,$msg,$filed);
           $validate->scene('fh', ['order_id','logistic_type_id','logisticCode']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }


    /**
     * 全部数据
     */
    public function lst($page = 15)
    {
        $admin_id = session('admin_id');
        $where = [];
        //除了管理员
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {//谁的店铺，谁看
//            $shop_id = Db::table('shop_shop')->where(['is_usable'=>1,'admin_id'=>$admin_id])->field('id')->find();
//            $shop_id = $shop_id['id'];
            $where['s.admin_id'] = $admin_id;
        }
        //其他条件
        $time = time();
        $year = date('Y',$time);

        $_order_data = Db::table('order_order_'.$year)
             ->alias('o')
             ->join('shop_shop s','s.id = o.shop_id ','LEFT')
             ->join('order_state os','os.id = o.state ','LEFT')
             ->field('o.order_id,o.shop_id,s.title as shop_name,o.total_price,o.is_fh,o.type,o.state as state_id,os.state,o.message,o.ps_attr,o.created_at')
             ->where($where)
             ->order('s.id,o.id desc')
             ->paginate($page);
        $data['page'] = $_order_data->render();
        $order_data = [];
        foreach($_order_data as $v)
        {
            //收集订单商品
            $v['order_goods'] = Db::table('order_goods_'.$year)->where(['order_id'=>$v['order_id']])->field('id,shop_name,shop_sm_icon,shop_attr,shop_num,shop_price')->select();
            $order_data[$v['shop_name']][] = $v;
        }
        $data['data'] = $order_data;
        return $data;
    }


    /**
     * 删除数据
     * @param $id
     */
    public function cancle($order_id)
    {
        $admin_id = session('admin_id');
        $where = [];
        $where['order_id'] = $order_id;
        $where['state'] = 1;//只有未付款的订单才可以取消
        //除了管理员
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {//谁的店铺，谁看
            $shop_id = Db::table('shop_shop')->where(['is_usable'=>1,'admin_id'=>$admin_id])->field('id')->find();
            $shop_id = $shop_id['id'];
            $where['shop_id'] = $shop_id;
        }
        $year = date('Y',time());
        return Db::table('order_order_'.$year)->where($where)->update(['state'=>4]);
    }


    /**
     * 发货
     * @param $data
     */
        public function fh($data)
        {
            $res = $this->_V('fh',$data);
            if($res !== true)
            {
                $this->code = -1;
                $this->msg = $res;
                return false;
            }

            $admin_id = session('admin_id');
            $where = [];
            $where['state'] = 2;//只有待收货的订单
            $where['order_id'] = $data['order_id'];
            $where['ps_attr'] = 1;
            $where['is_fh'] = 0;

            //除了管理员
            $adminModel = new AdminM();
            $res = $adminModel->isAdmin($admin_id);
            if($res === false)
            {//谁的店铺，谁看
                $shop_id = Db::table('shop_shop')->where(['is_usable'=>1,'admin_id'=>$admin_id])->field('id')->find();
                $shop_id = $shop_id['id'];
                $where['shop_id'] = $shop_id;
            }
            $year = date('Y',time());
            $res = Db::table('order_order_'.$year)->where($where)->update(['is_fh'=>1,'logisticCode'=>$data['logisticCode'],'logistic_type_id'=>$data['logistic_type_id']]);
            if(!$res)
            {
                $this->code = -1;
                $this->msg = '发货失败';
                return false;
            }
            return true;
        }


        public function kdsearch($order_id)
        {
            $year = date('Y',time());
            $order_data = Db::table('order_order_'.$year)->where('order_id',$order_id)->field('order_id,logisticCode,logistic_type_id')->find();
            //物流类型
            $ldata = Db::table('order_logistic_type')->field('code,name')->find($order_data['logistic_type_id']);
            //查看物流信息
            $kd = new KdSearch();
            $wl = $kd->getOrderTracesByJson($order_id,$ldata['code'],$order_data['logisticCode']);
            $wl = json_decode($wl,true);
            $_wl = array_reverse($wl['Traces']);
            unset($wl['Traces']);
            $wl['Traces'] = $_wl;
            $data['wl'] = $wl;
            $data['wl_name'] = $ldata['name'];
            return $data;
        }


}
